﻿using System;
using System.Text;
using System.Web;
using System.Web.Configuration;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;
using DataObject.Product;

public partial class Controls_Article_Left_Column : System.Web.UI.UserControl
{
    readonly StringBuilder _sb = new StringBuilder();
    public int TypeId { get; set; }
    protected int CategoryId;
    protected int ProductId;
    protected int ArticleId;
    protected string ActiveMenu = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        CategoryId = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
        ProductId = Utils.GetIdFromRequestString(Utils.ProductId, 0);
        ArticleId = Utils.GetIdFromRequestString(Utils.ArticleId, 0);

        if (CategoryId > 0)
        {
            try
            {
                var category = CategoryController.Category_GetById(CategoryId);
                if (category != null)
                {
                    if (category.ParentId == 0)
                    {
                        if (category.Priority - 1 >= 0)
                            ActiveMenu = (category.Priority - 1).ToString();
                    }
                    else
                    {
                        var subCategory = CategoryController.Category_GetById(category.ParentId);
                        if (subCategory != null)
                        {
                            if (subCategory.Priority - 1 >= 0)
                                ActiveMenu = (subCategory.Priority - 1).ToString();
                        }
                    }
                }
            }
            catch { }
        }

        BuildMenuArticle();
        ltCategory.Text = _sb.ToString();
    }

    private void BuildMenuArticle()
    {
        var categories = CategoryController.Category_GetByParentId_Filter(false, (int)CategoryType.Article, Constants.PARENT_ROOT, Constants.ACTIVE);
        if (categories.Count > 0)
        {
            for (int i = 0; i < categories.Count; i++)
            {

                _sb.AppendFormat("<li><a class=\"menuheader expandable\">{0}</a>", categories[i].Name);

                var subCategory = CategoryController.Category_GetByParentId((int)CategoryType.Article, categories[i].CategoryId, Constants.ACTIVE);
                if (subCategory.Count > 0)
                {
                    _sb.Append("<ul class=\"categoryitems\">");
                    foreach (CategoryObject t in subCategory)
                    {
                        _sb.AppendFormat(
                            t.CategoryId == CategoryId
                                ? "<li style=\"float:left;margin-left:20px;width:100%\"><a href=\"{0}\" style=\"font-weight: bold; color: #ff4a4a\">{1}</a></li>"
                                : "<li style=\"float:left;margin-left:20px;width:100%\"><a href=\"{0}\">{1}</a></li>",
                            UrlHelpers.ListArticleByCategory(ConvertUtility.ToInt32(t.CategoryId), t.Url),
                            t.Name);
                    }
                    _sb.Append("</ul>");
                }
                _sb.Append("</li>");
            }
        }
    }
}